package Utils;

import annotation.Controller;
import annotation.RequestMapping;
import component.Ioc;

import java.io.File;
import java.lang.reflect.Method;

public class LoadClassController{
    protected static void dealDir(String filepath) throws ReflectiveOperationException {
        File file = new File(LoadClassBean.class.getResource(filepath).getPath());
        String[] names = file.list();
        for(String str : names){
            str = filepath+str;
            file = new File(LoadClassBean.class.getResource(str).getPath());
            if(file.isDirectory()){
                dealDir(str+"/");
            }else{
                if(str.endsWith(".class")){
                    str = str.substring(1,str.length()-6).replaceAll("/",".");
                    dealClass(str);
                }
            }
        }
    }
    public static void dealClass(String filepath) throws ReflectiveOperationException {
        Class<?> klass = Class.forName(filepath);
        Controller controller;
        if((controller = klass.getAnnotation(Controller.class)) != null) {
            ControllerComponent controllerComponent = ControllerComponent.getControllerComponent();
            Method[] methods = klass.getMethods();
            for (Method method : methods) {
                System.out.println(method.toString());
                RequestMapping requestMapping = method.getDeclaredAnnotation(RequestMapping.class);
                if (requestMapping == null) continue;
                System.out.println(controller.value()+requestMapping.value());
                controllerComponent.insertController(controller.value()+requestMapping.value(), Ioc.getBean(controller.value()));
                break;
            }
        }
    }
}
